var express = require('express');
var formidable = require('formidable')
var path = require('path')
var router = express.Router();

/* GET home page. */
router.get('/', function (req, res, next) {
  res.render('index', {
    title: 'Express'
  });
});
router.get('/portrait', (req, res) => {
  res.render('portrait', {
    title: 'testUpload'
  })
})
router.post('/portrait', (req, res, next) => {
  const form = formidable({
    multiples: true,
    uploadDir: path.resolve(__dirname, '../public/images'),
    keepExtensions: true
  });

  form.parse(req, (err, fields, files) => {
    if (err) {
      next(err);
      return;
    }
    let imageurl = '/images/' + files.portrait.newFilename //将来将该路径存储到数据库中
    res.send(imageurl)
  });
})
module.exports = router;